#include "Ray.h"

Ray::Ray() {
    //nothing
}

Ray::Ray(const Vector3f& origin, const Vector3f& dir, float maxT) {
    this->origin = origin;
    this->dir = dir;
    this->maxT = maxT;
}

Vector3f Ray::point(float t) const {
    return origin + t * dir;
}

bool Ray::isInRange(float t) const {
    return t > 0.0 && (t < maxT || maxT < 0.0);
}

bool Ray::timeInRect(float& minT, float& maxT, float minX, float maxX, float minY, float maxY) const {
    float minTx = (minX - origin.x) / dir.x;
    float maxTx = (maxX - origin.x) / dir.x;
    if (minTx > maxTx) swap(minTx, maxTx);
    if (maxTx < 0.0) return false;

    float minTy = (minY - origin.y) / dir.y;
    float maxTy = (maxY - origin.y) / dir.y;
    if (minTy > maxTy) swap(minTy, maxTy);
    if (maxTy < 0.0) return false;

    minT = max(minTx, minTy);
    maxT = min(maxTx, maxTy);

    if (minT > maxT) return false;

    if (minT < 0.0) minT = 0.0;
    return true;
}

float Ray::timeToX(float x) const {
    return (x - origin.x) / dir.x;
}

float Ray::timeToY(float y) const {
    return (y - origin.y) / dir.y;
}
